/*!
 * Ext JS Library 3.1.1
 * Copyright(c) 2006-2010 Ext JS, LLC
 * licensing@extjs.com
 * http://www.extjs.com/license
 */
CzHouseGrid = function(viewer, config) {
    this.viewer = viewer;
    Ext.apply(this, config);

    this.store = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: '/desktop/get_czxx'
        }),

        reader: new Ext.data.JsonReader({
          totalProperty: 'results', 
          root: 'rows',             
          fields: [
          {name: 'bgmj',  type: 'string'},
          {name: 'bz',  type: 'string'},
          {name: 'cfmj',  type: 'string'},
          {name: 'cq',  type: 'string'},
          {name: 'create_at', type: 'date', dateFormat: 'Y-m-d H:i:s'},
          {name: 'cs',  type: 'string'},
          {name: 'czbh',  type: 'string'},
          {name: 'czfs',  type: 'string'},
          {name: 'dgl', type: 'string'},
          {name: 'dzwz',  type: 'string'},
          {name: 'fwcq',  type: 'string'},
          {name: 'fwly',  type: 'string'},
          {name: 'fwpt',  type: 'string'},
          {name: 'fwss',  type: 'string'},
          {name: 'fwst',  type: 'string'},
          {name: 'fwwq',  type: 'string'},
          {name: 'fwxz',  type: 'string'},
          {name: 'fwzj',  type: 'string'},
          {name: 'fyfs',  type: 'string'},
          {name: 'glf', type: 'string'},
          {name: 'gs',  type: 'string'},
          {name: 'yzk', type: 'string'},
          {name: 'height',type: 'string'},
          {name: 'id'},
          {name: 'jfsj',  type: 'string'},
          {name: 'jy',  type: 'string'},
          {name: 'kdmj',  type: 'string'},
          {name: 'process', type: 'string'},
          {name: 'khdh',  type: 'string'},
          {name: 'khdz',  type: 'string'},
          {name: 'khgj',  type: 'string'},
          {name: 'khlxr', type: 'string'},
          {name: 'khsfz', type: 'string'},
          {name: 'khxm',  type: 'string'},
          {name: 'kh_id'},
          {name: 'lc',  type: 'string'},
          {name: 'length',type: 'string'},
          {name: 'lx',  type: 'string'},
          {name: 'mpdh',  type: 'string'},
          {name: 'pq',  type: 'string'},
          {name: 'sdj', type: 'float'},
          {name: 'ssmj',  type: 'string'},
          {name: 'szj', type: 'float'},
          {name: 'tax', type: 'string'},
          {name: 'update_at', type: 'date', dateFormat: 'Y-m-d H:i:s'},
          {name: 'width',type: 'string'},
          {name: 'wlf', type: 'string'},
          {name: 'wtbh',  type: 'string'},
          {name: 'wtfs',  type: 'string'},
          {name: 'wtdq', type: 'date', dateFormat: 'Y-m-d H:i:s'},
          {name: 'wymc',  type: 'string'},
          {name: 'wyzd_id'},
          {name: 'ygbm',  type: 'string'},
          {name: 'ygxm',  type: 'string'},
          {name: 'yg_id'},
          {name: 'yt',  type: 'string'},
          {name: 'yzf', type: 'string'},
          {name: 'zc',  type: 'string'},
          {name: 'zdj', type: 'float'},
          {name: 'zmj', type: 'string'},
          {name: 'zt',  type: 'string'},
          {name: 'zzj', type: 'float'},
          {name: 'lxfs',  type: 'string'},
          {name: 'contract_id'}        
          ]    
        }),
        sortInfo:{field: 'id', direction: "DESC"},
        baseParams: {start:0, limit:15, query:""}
    });
    
    
    this.store.reload();
    
    this.store.setDefaultSort('update_at', "DESC");


    this.columns = [
      { id: 'id', header: "id", dataIndex: 'id', hidden: true, width: 25,sortable: true},
      { header: "!",      dataIndex: 'process',  width: 20, sortable:true},
      { header: "房源编号",   dataIndex: 'czbh',  width: 150, sortable:true},
      { header: "交易",     dataIndex: 'jy',    width: 75, sortable: true},
      { header: "城区",     dataIndex: 'cq',    width: 100, sortable: true},
      { header: "片区",     dataIndex: 'pq',    width: 100, sortable: true, hidden: true},
      { header: "厂房名称",   dataIndex: 'wymc',  width: 250, sortable: true},
      { header: "地理位置",   dataIndex: 'dzwz',  width: 250, sortable: true},
      { header: "栋数/栋号",     dataIndex: 'mpdh',   width: 75, sortable:true},
      { header: "楼层",     dataIndex: 'lc',    width: 75, sortable:true},
      { header: "总层",     dataIndex: 'zc',    width: 150, sortable:true},
      { header: "面积",     dataIndex: 'zmj',   width: 100, sortable:true},
      { header: "层数",     dataIndex: 'cs',    width: 100, sortable:true},
      { header: "售总价",    dataIndex: 'szj',   width: 120, sortable:true},
      { header: "租总价",    dataIndex: 'zzj',   width: 120, sortable:true},
      { header: "委托日",    dataIndex: 'create_at',   width: 200, sortable:true, renderer: Ext.util.Format.dateRenderer('Y-m-d')},
      { header: "部门",     dataIndex: 'ygbm',    width: 150, sortable:true},
      { header: "员工",     dataIndex: 'ygxm',    width: 150, sortable:true},
      { header: "最后跟进日",  dataIndex: 'update_at',   width: 200, sortable:true, renderer: Ext.util.Format.dateRenderer('Y-m-d')}
    ];

    CzHouseGrid.superclass.constructor.call(this, {
        region: 'center',
        id: 'cz-house-grid',
        loadMask: {msg:'正在载入...'},

        sm: new Ext.grid.RowSelectionModel({
            singleSelect:true
        }),

        stripeRows:true,
        
        viewConfig: {
            forceFit:true,
            enableRowBody:true,
            showPreview:true,
            getRowClass : this.applyRowClass
        }
    });

    this.on('rowcontextmenu', this.onContextClick, this);
};

var cz_click_handler = function(item, e) {
    var pars = {id:czxx_id, name:item.text};
    new Ajax.Request("/desktop/trans_czxx", { 
      method: "POST",
        parameters: pars,
        onComplete:  function(request) {
          Ext.getCmp('czxx-view').reloadStore();
        }
    });   
}

var cz_otherMenu = new Ext.menu.Menu({
    id: 'cz_other_person',
    items:[{text:'王娅红', iconCls: 'user', handler:cz_click_handler},{text:'胡启龙', iconCls: 'user', handler:cz_click_handler},{text:'汪玲玲', iconCls: 'user', handler:cz_click_handler},{text:'夏小丽', iconCls: 'user', handler:cz_click_handler},{text:'公共帐户', iconCls: 'user', handler:cz_click_handler},{text:'李焱', iconCls: 'user', handler:cz_click_handler},{text:'程慧杰', iconCls: 'user', handler:cz_click_handler},{text:'朱媛媛', iconCls: 'user', handler:cz_click_handler},{text:'许萍', iconCls: 'user', handler:cz_click_handler},{text:'陶淑君', iconCls: 'user', handler:cz_click_handler},{text:'王洪利', iconCls: 'user', handler:cz_click_handler},{text:'陆永迪', iconCls: 'user', handler:cz_click_handler},{text:'许青', iconCls: 'user', handler:cz_click_handler},{text:'刘华影', iconCls: 'user', handler:cz_click_handler},{text:'刘主管', iconCls: 'user', handler:cz_click_handler},{text:'朱主管', iconCls: 'user', handler:cz_click_handler}]
});

Ext.extend(CzHouseGrid, Ext.grid.EditorGridPanel, {

    onContextClick : function(grid, index, e){
        if(!this.menu){ // create context menu on first right click
          //  new Ajax.Request("/desktop/get_czuser_menu", {
          //    method: "POST",
          //    onComplete:  function(request) {
          //       var qz_menu = Ext.getCmp('cz_other_person');
          //         ss=eval(request.responseText);
          //         for (var i=0; i < ss.size(); i ++) {
          //            qz_menu.addItem(ss[i]);
          //         }
          //      }
          //  });
            
            this.menu = new Ext.menu.Menu({
                id:'cz-task-ctx',
                items: [{
                    iconCls: 'new-win',
                    text: '转他人',
                    menu: cz_otherMenu
                },'-',{
                    iconCls: 'refresh',
                    text:'刷新',
                    scope:this,
                    handler: function(){
                      this.ctxRow = null;
                      this.store.reload();
                    }
                },{
                  iconCls:'delete',
                  text:'删除记录',
                  scope:this,
                  handler: function(){
                      //var pars = "id=" + this.ctxRecord.id;
                      var pars = {id:this.ctxRecord.id};
                      var ctxRow = this.ctxRow;
                      var cur_store = this.store;
                      Ext.Msg.confirm('信息','确定要删除？', function(btn){
                        if (btn == 'yes') {
                          //var pars = {id:this.ctxRecord.id};
                          new Ajax.Request("/desktop/delete_czxx", { 
                            method: "POST",
                              parameters: pars,
                              onComplete:  function(request) {
                                //this.ctxRow = null;
                                cur_store.reload();
                              }
                          });                         
                        }
                      });
                  }   
                }]
            });
            this.menu.on('hide', this.onContextHide, this);
        }
        e.stopEvent();
        if(this.ctxRow){
            Ext.fly(this.ctxRow).removeClass('x-node-ctx');
            this.ctxRow = null;
        }
        this.ctxRow = this.view.getRow(index);
        this.ctxRecord = this.store.getAt(index);
        Ext.fly(this.ctxRow).addClass('x-node-ctx');

        if (currentUser.prev_code[11] != 't')  {        
          Ext.getCmp('cz-task-ctx').items['items'][3].hide();
        } else {
          Ext.getCmp('cz-task-ctx').items['items'][3].show();
        }
        
        if　( currentUser.id != parseInt (this.ctxRecord.data.yg_id) ) {
          Ext.getCmp('cz-task-ctx').items['items'][3].hide();
        } else {
          Ext.getCmp('cz-task-ctx').items['items'][3].show();
        }


        if (currentUser.prev_code[14] != 't') {
          Ext.getCmp('cz-task-ctx').items['items'][0].hide();
          Ext.getCmp('cz-task-ctx').items['items'][1].hide();
        } else {
          Ext.getCmp('cz-task-ctx').items['items'][0].show();
          Ext.getCmp('cz-task-ctx').items['items'][1].show();
          Ext.getCmp('cz-task-ctx').items['items'][3].show();         
        }

        this.menu.showAt(e.getXY());
    },

    onContextHide : function(){
        if(this.ctxRow){
            Ext.fly(this.ctxRow).removeClass('x-node-ctx');
            this.ctxRow = null;
        }
    },

    loadData : function(paras) {
        this.store.baseParams = paras;
        this.store.load();
    },

    formatDate : function(date) {
        if (!date) {
            return '';
        }
        var now = new Date();
        var d = now.clearTime(true);
        var notime = date.clearTime(true).getTime();
        if (notime == d.getTime()) {
            return 'Today ' + date.dateFormat('g:i a');
        }
        d = d.add('d', -6);
        if (d.getTime() <= notime) {
            return date.dateFormat('D g:i a');
        }
        return date.dateFormat('n/j g:i a');
    },

    formatTitle: function(value, p, record) {
        return String.format(
                '<div class="topic"><b>{0}</b><span class="author">{1}</span></div>',
                value, record.data.author, record.id, record.data.forumid
                );
    },

    formatkey: function(value) {
      if (value == '有匙') {
        return "<img src='/images/k_2.png' />";
      } else {
        return "";
      }
    }
});

Ext.reg('czhousegrid', CzHouseGrid);